﻿// License: https://opensource.org/licenses/MIT
// Create Date : 2018-01-01

using System;
using System.Text;

namespace Cat.Core.XLog.LogToText
{
    /// <summary>
    /// 日志记录到StringBuilder的ILogger(主要用来做单元测试)
    /// </summary>
    public sealed class TextLogger : LoggerBase, ILogger
    {
        public string Name { get; }
        public TextLogger(string name)
        {
            ClearText();
            Name = name;
        }

        StringBuilder _text;
        public string Text { get { return _text.ToString(); } }
        public void ClearText() { _text = new StringBuilder(); }

        protected override void Log(LogLevel level, Exception exception, Lazy<string> message)
        {
            if (exception != null)
            {
                _text.AppendFormat("{0},Message='{1}'\r\nExecption='{2}'\r\n{3}\r\n", level, message.Value, exception.Message, exception.StackTrace);
            }
            else
            {
                _text.AppendFormat("{0},Message='{1}'\r\n", level, message.Value);
            }
        }
    }
}
